from utils.celery import app  # 导入上面配置的Celery实例
from ronglian_sms_sdk import SmsSDK
import json
import logging

# 容联云配置（替换为你的实际账号信息）
accId = '2c94811c946f6bfb0196ec6aae997844'
accToken = 'cc1de311989f4c8e99b418368a6140ef'
appId = '2c94811c946f6bfb0196ecbe862f794a'
sdk = SmsSDK(accId, accToken, appId)

@app.task
def send_sms_task(phone: str, code: str, valid_time: int):
    """异步发送短信验证码"""
    try:
        tid = '1'  # 容联云短信模板ID（需提前在平台申请）
        datas = (code, str(valid_time))  # 模板参数：验证码、有效期
        resp = sdk.sendMessage(tid, phone, datas)  # 调用容联云API
        resp_data = json.loads(resp)
        
        if resp_data.get('statusCode') == '000000':
            logging.info(f"短信发送成功：{phone}")
            return True
        else:
            logging.error(f"发送失败：{resp_data.get('statusMsg')}")
            return False
    except Exception as e:
        logging.error(f"发送异常：{str(e)}")
        return False